package com.example.administrator.leargit.activity;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.administrator.leargit.R;
import com.example.administrator.leargit.fragment.Basefragment;

/**
 * Created by Administrator on 2016/12/22 0022.
 */
public class BaseActivity extends FragmentActivity {

    private  GestureDetector mDetecter;
    private long touchTime = 0;
    protected FragmentManager fragmentManager;
    boolean isLock=false;

    public boolean isLock() {
        return isLock;
    }

    public void setLock(boolean lock) {
        isLock = lock;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initData(getIntent().getExtras());
        super.onCreate(savedInstanceState);
        fragmentManager=getSupportFragmentManager();
        requestWindowFeature(Window.FEATURE_NO_TITLE);





    }

    public void setTitle(String title) {

    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        initData(intent.getExtras());
    }

    private void initData(Bundle extras) {

    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        initSystemBar();//初始系统的视图

    }

    private void initSystemBar() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);//透明状态
        }
        mDetecter = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                float path = Math.abs(e2.getRawX() - e1.getRawX());
              /*  if (e1.getRawX() < WIDTH && path > SCREEN_WIDTH / 2) {// 右滑

                } else if (e1.getRawX() > SCREEN_WIDTH - WIDTH && path > SCREEN_WIDTH / 2) {

                }*/
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });
    }

    public void setmDetecter(GestureDetector mDetecter) {
        this.mDetecter = mDetecter;
    }
    /**
     * 事件分发机制
     * */
    public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
        mDetecter.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    public void onBackPressed() {

            long currentTime = System.currentTimeMillis();
            if ((currentTime - touchTime) >= 2000) {
               /* UIUtils.show("再按一次退出应用");*/
                touchTime = currentTime;
            } else {
                finish();
                /*StoneApp.exit();*/
            }
    }

    protected Basefragment addFragment(int id,Basefragment fragment,boolean isAnim){
        fragment.setTAG("");
        initData(fragment.getArguments());
        try {
            FragmentTransaction transaction = getFragmentTransaction(isAnim);
            transaction.add(id,fragment,fragment.getTAG());
        } catch (Exception e) {
            e.printStackTrace();
            FragmentTransaction transaction = getFragmentTransaction(isAnim);
            transaction.add(id, fragment, fragment.getTAG()).addToBackStack(fragment.getTAG()).commitAllowingStateLoss();
        }
        return fragment;
    }
    protected void addFragment(Intent intent){
        Class<?extends Basefragment> clazz= (Class<? extends Basefragment>) intent.getSerializableExtra(Intent.EXTRA_PACKAGE_NAME);
        try {
            Basefragment basefragment=clazz.newInstance();
            basefragment.setArguments(intent.getExtras());
            addFragment(0,basefragment,false);
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }

    }
    public FragmentTransaction getFragmentTransaction(boolean isAnim) {
       FragmentTransaction transaction =fragmentManager.beginTransaction();
        if (isAnim) {
           /* transaction.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left, R.anim.slide_in_from_left, R.anim.slide_out_to_right);*/
        }
        return transaction;
    }
    public Basefragment removeFragment(Basefragment fragment) {
        try {
            //允许减少
            getSupportFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
        } catch (Exception e) {
//			e.printStackTrace();
        }
        return fragment;
    }
    public Basefragment replaceFragment(int id ,Basefragment fragment) {
        fragment.setTAG("");
        initData(fragment.getArguments());
        FragmentTransaction transaction = getFragmentTransaction(false);
        transaction.add(id, fragment, fragment.getTAG()).addToBackStack(fragment.getTAG()).commitAllowingStateLoss();
        return fragment;
    }
}
